package com.jxaummd.mushroom.main.common.presenter;

import android.os.Handler;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.core.app.application.University;
import com.jxaummd.mushroom.core.base.data.CommonBackModel;
import com.jxaummd.mushroom.core.base.data.DataSource;
import com.jxaummd.mushroom.core.base.presenter.BasePresenter;
import com.jxaummd.mushroom.core.util.tool.timer.BaseTimerTask;
import com.jxaummd.mushroom.core.util.tool.timer.ITimerListener;
import com.jxaummd.mushroom.main.common.contract.SignUpContract;
import com.jxaummd.mushroom.main.common.data.RegisterData;

import java.util.Timer;

public class SignUpPresenter extends BasePresenter<SignUpContract.View>
        implements SignUpContract.Presenter, ITimerListener, DataSource.Callback<CommonBackModel> {
    // 定时任务
    private BaseTimerTask mBaseTimerTask;
    //定时者
    private Timer mTimer;

    private Handler mHandler = University.getHandler();

    private static String mHintMessage = Utils.getApp().getResources().getString(R.string.common_signup_testcode_time_hint);

    private int mCount = 60;

    public SignUpPresenter(SignUpContract.View view) {
        super(view);
    }

    @Override
    public void start() {
        super.start();
        initTimer();
    }

    /**
     * 初始化定时器
     */
    private void initTimer() {
        mTimer = new Timer();
        mBaseTimerTask = new BaseTimerTask(this);
        mTimer.schedule(mBaseTimerTask, 0, 1000);
    }

    @Override
    public void onTimer() {
        if (getView() != null) {
            getView().updateTime(mCount +"s"+ mHintMessage);
            mCount--;
            if (mCount < 0) {
                mTimer.cancel();
            }
        }
    }

    @Override
    public void register(String phoneNumber, String password, String code) {
            if(phoneNumber.length()==11){
                RegisterData.register(phoneNumber,password,code,this);
            }
    }

    @Override
    public void onDataNotAvailable(int strRes) {

    }

    @Override
    public void onDataLoaded(CommonBackModel commonBackModel) {
        ToastUtils.showShort(commonBackModel.getMessage());
        if(commonBackModel.getStatus()==200){
            getView().registerSuccess();
        }
    }
}
